// pages/index/index.js
var WxParse = require('../../lib/wxParse/wxParse.js');
var WxParse = require('../../lib/wxParse/wxParse.js');
Page({
  data:{
    swiperHeight:0,
    categoryId:1,
    schoolDetail:null,
    isFull: false   //用户数据是否完整
    
  },
   //分享功能
   onShareAppMessage:function(){
    return {
      title: '油菜花',
      desc: '油菜花',
      path: '/page/school/list'
    }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        wx.createSelectorQuery().select('#tab').boundingClientRect(function (rect) {
          that.setData({
            swiperHeight:res.windowHeight - rect.bottom
          })
        }).exec()
        
      }
    });
    console.log(options.schoolid);
    this.setData({ schoolid: options.schoolid})
    this.loadSchollDetail({ universityId: options.schoolid, pageNum: 1, pageSize: 10 });
    
  
  },
  
  onReady:function(){
    // 页面渲染完成
   
  },
  onShow:function(){
    // 页面显示
    if (typeof (this.data.userDataSave) != "undefined" && this.data.userDataSave == true) {
      wx.showToast({
        title: "报名成功",
        icon: 'success',
        duration: 3000,
        mask: true
      })
      this.setData({ userDataSave: false });

    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  changeCategory: function (e){
    
    this.setData({ categoryId: e.target.dataset.categoryid });
    //TODO 在这里加载数据
    
  },
  swiperChangeInfo: function (e){
    console.log(e.detail.current);
    this.loadSchollDetail({ universityId: e.target.dataset.universityid, currentNum: e.detail.current,pageNum: 1, pageSize: 10 });
  },
  swiperChangeCategory: function(e){
    console.log(e.detail.current);
    this.setData({
      categoryId: e.detail.current + 1
    });
  },
  loadSchollDetail: function (requestObj) {
    var self = this;
    wx.request({
      url: getApp().globalData.server + "/university/university_detail.do",
      data: requestObj,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.status != 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }else {
          console.log(requestObj);
          console.log(res.data);
          self.setData({
            schoolDetail: res.data.data.list[0]
          });
          if (res.data.data.list[0].introduce != null){
            var university_detail = res.data.data.list[0].introduce.detail;
            WxParse.wxParse('university_detail', 'html', university_detail, self, 5);
          }
         
          
        }

      },
      fail: function () {
        wx.showToast({
          title:"获取学校详情失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }
    })
  },
  openMajorDetailWindow:function(e){
    wx.navigateTo({
      url: '/page/majar/majar_detail?majorid=' + e.currentTarget.dataset.majorid + '&schoolid=' + e.currentTarget.dataset.schoolid,
    })
  },
  enroll:function(){
    var self = this;
    getApp().login(function () {
      if (self.data.isFull || getApp().globalData.hasLogin) {
        wx.showToast({
          title: "报名成功",
          icon: 'success',
          duration: 2000,
          mask: true
        })
        //TODO 报名信息持久化
      }
      else {
        wx.navigateTo({
          url: '/page/my/account'
        })
      }

    });
  },
  openWebView:function(e){
    console.log("url=" + e.currentTarget.dataset.url);
    wx.navigateTo({
      url: '/page/index/webview?title=' + e.currentTarget.dataset.title + '&url=' + e.currentTarget.dataset.url
    })
  },
  makePhoneCall:function(e){
    console.log(e.currentTarget.dataset.phonenumber);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phonenumber //仅为示例，并非真实的电话号码
    })
  }
})